package com.qf.springboottest.controller;

import com.baomidou.kaptcha.Kaptcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class KaptchaController {

    /**
     * 依赖 验证码 核心对象
     * */
    @Autowired
    private Kaptcha kaptcha;

    @GetMapping("/captcha")
    public void render() {
        log.debug("获取验证码图片");
        kaptcha.render();  // 生成验证码图片，并返回
    }

    @GetMapping("/valid")
    public void validDefaultTime(@RequestParam String code) {
        //default timeout 900 seconds
        kaptcha.validate(code);
    }

    @GetMapping("/validTime")
    public void validCustomTime(@RequestParam String code) {
        kaptcha.validate(code, 100);
    }
}
